<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if($_SESSION['os']=='windows'){
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
		if(isset($_RQ['mid'])){
			$params['mid']=intval($_RQ['mid']);
			$return['params']['mid']=$_RQ['mid'];
		}
		if(isset($_RQ['status'])){
			$params['status']=intval($_RQ['status']);
			$return['params']['status']=$_RQ['status'];
		}
		//创建时间
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
			$return['params']['createtime']=$time_range;
		}
		$page=intval($_RQ['page']);
		if(!$page){
			$page=1;
		}
		$limit=array($page,10);
		$list=pdo_getslice('jy_peimilk_baodan',$params,$limit,$total,'','','id desc');
		if($list){
			foreach($list as &$item){
				$item['member']=cfc('core')->member_show($item['mid']);
				$goods=json_decode($item['goods'],true);
				$item['total']=$goods[0]['total'];
				$item['title']=$goods[0]['title'];
				$item['image']=$goods[0]['image'];
			}
		}
		$totalpage=ceil($total/$psize);
		$totalpage=intval($totalpage);
		$return['totalpage'] =$totalpage;
		$return['params']['page'] =$pindex;
		$return['list']=$list;
		$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'报单列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'total'=>$total,'psize'=>$psize,'list'=>array(
				array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
				array('name'=>'realname','title'=>'收件人_Realname','type'=>'string'),
				array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
				'data'=>array(
					array('title'=>'','name'=>'title'),
					array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
					)
				),
				array('name'=>'price','title'=>'金额','type'=>'string'),
				array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'baopaytype'),
				array('name'=>'ispay','title'=>'是否已支付','type'=>'radio','options'=>'ispay'),
				array('name'=>'createtime','title'=>'时间','type'=>'time'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'baodanstatus'),
			),
			'action'=>array(
				array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
				array('type'=>'modal','op'=>'checkpay','title'=>'确认支付','show'=>array('ispay'=>0)),
				array('type'=>'modal','op'=>'look','title'=>'查看详情','show'=>array('status'=>0)),
			)),
			'search'=>array(
				array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'baodanstatus'),
				array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'core/member.get_member/'),
				array('name'=>'createtime','width'=>15,'title'=>'下单时间','type'=>'time_range'),
			)
		);
		$return['htmls']=$htmls;
		$return['baodanstatus']=$this->baodanstatus;
		$return['baopaytype']=$this->baopaytype;
		$return['ispay']=array('未支付','已支付');
	}else{
		$return['list']=array();
	}
	$return['params']['status']=0;
	exi($return);
}
if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$status=intval($_RQ['status']);
	$params['status']=$status;
	$page=intval($_RQ['page']);
	if(!$page){
		$page=1;
	}
	$limit=array($page,10);
	$list=pdo_getslice('jy_peimilk_baodan',$params,$limit,$total,'','','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['statustitle']=$this->baodanstatus[$item['status']];
			$item['goods']=json_decode($item['goods'],true);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_peimilk_baodan',array('id'=>$id));
	$item['member']=cfc('core')->member_show($item['mid']);
	$item['goods']=json_decode($item['goods'],true);
	$htmls=array(
		'type'=>'look',
		'desc'=>'查看',
	);
	$htmls['look']=array(
				array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
				array('name'=>'realname','title'=>'收件人_Realname','type'=>'string'),
				array('name'=>'price','title'=>'金额','type'=>'string'),
				array('name'=>'createtime','title'=>'时间','type'=>'time'),
	);
	foreach($item['goods'] as $k=>$gv){
				$num=$k+1;
				$item['image'.$k]=$gv['image'];
				$htmls['look'][]=array('type'=>'img_string','title'=>'商品'.$num,'img'=>'image'.$k,
				'data'=>array(
					array('title'=>$gv['title']),
					array('title'=>'数量:'.$gv['total'])
					)
				);
	}
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='check'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$post=get_postdata();	
		$id=intval($post['id']);
		pdo_update('jy_peimilk_baodan',array('status'=>intval($post['status']),'checkmsg'=>$post['checkmsg']),array('id'=>$id));
		if($post['status']==1){
			$this->model('order')->baodan_check($id);
		}
		exi('操作成功！','','main');	
	}
	$item=pdo_get('jy_peimilk_baodan',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核',
	);
	$htmls['edit']=array(
		//array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'baopaytype'),
		//array('name'=>'ispay','title'=>'是否已支付','type'=>'radio','options'=>'ispay'),
		array('type'=>'radio','title'=>'审核','name'=>'status','options'=>'baodanstatus'),
		array('type'=>'string','title'=>'审核备注','name'=>'checkmsg','help'=>''),
	);
	$return['htmls']=$htmls;
	$return['baodanstatus']=$this->baodanstatus;
	$return['baopaytype']=$this->baopaytype;
	$return['ispay']=array('未支付','已支付');
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='checkpay'){
	$id=intval($_RQ['id']);
	if($_RQ['postdata']){
		$post=get_postdata();	
		$id=intval($post['id']);
		pdo_update('jy_peimilk_baodan',array('ispay'=>intval($post['ispay']),'paytype'=>$post['paytype']),array('id'=>$id));
		exi('操作成功！','','main');	
	}
	$item=pdo_get('jy_peimilk_baodan',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核',
	);
	$htmls['edit']=array(
		array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'baopaytype'),
		array('name'=>'ispay','title'=>'是否已支付','type'=>'radio','options'=>'ispay'),
	);
	$return['htmls']=$htmls;
	$return['ispay']=array('未支付','已支付');
	$return['baopaytype']=$this->baopaytype;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_peimilk_baodan',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}